package com.github.aloxc.plugin.restplus.component.listener;


import com.github.aloxc.plugin.restplus.component.VTree;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.listener.TreeNodeMenuItemActionListener;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Map;

/**
 * VTREE 显示复制等的菜单
 */
public class VTreeMouseListener implements MouseListener {
    private VTree responseTree;
    private Map jsonEleTreeMap ;

    public VTreeMouseListener(VTree responseTree, Map jsonEleTreeMap) {
        this.responseTree = responseTree;
        this.jsonEleTreeMap = jsonEleTreeMap;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {

        TreePath path = responseTree.getPathForLocation(e.getX(), e.getY());
        if (path == null) {
            return;
        }
        responseTree.setSelectionPath(path);
        DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) responseTree.getLastSelectedPathComponent();
        if (e.isPopupTrigger()) {
            JPopupMenu popMenu = new JPopupMenu();
            JMenuItem copyKey = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.response.tree.copy.key.text"));
            JMenuItem copyValue = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.response.tree.copy.value.text"));
            JMenuItem copyPath = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.response.tree.copy.path.text"));
            JMenuItem copyKeyValue = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.response.tree.copy.keyvalue.text"));
            JMenuItem copyNode = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.response.tree.copy.node.text"));
            JMenuItem copyPathAllVal = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.response.tree.copy.samepathvalue.text"));
            JMenuItem copySingleNodeString = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.response.tree.copy.map.text"));
            JMenuItem copyNodeFormat = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.response.tree.copy.nodeformatvalue.text"));


            popMenu.add(copyKey);
            popMenu.add(copyValue);
            popMenu.add(copyPath);
            popMenu.add(copyNode);
            popMenu.add(copyKeyValue);
            popMenu.add(copySingleNodeString);
            popMenu.add(copyPathAllVal);
            popMenu.add(copyNodeFormat);
            copyKey.addActionListener(new TreeNodeMenuItemActionListener(jsonEleTreeMap,responseTree, 1, selNode));
            copyValue.addActionListener(new TreeNodeMenuItemActionListener(jsonEleTreeMap,responseTree, 2, selNode));
            copyKeyValue.addActionListener(new TreeNodeMenuItemActionListener(jsonEleTreeMap,responseTree, 3, selNode));
            copyPath.addActionListener(new TreeNodeMenuItemActionListener(jsonEleTreeMap,responseTree, 4, path));
            copyPathAllVal.addActionListener(new TreeNodeMenuItemActionListener(jsonEleTreeMap,responseTree, 5, selNode));
            copyNode.addActionListener(new TreeNodeMenuItemActionListener(jsonEleTreeMap,responseTree, 6, path));
            copyNodeFormat.addActionListener(new TreeNodeMenuItemActionListener(jsonEleTreeMap,responseTree, 7, path));
            copySingleNodeString.addActionListener(new TreeNodeMenuItemActionListener(jsonEleTreeMap,responseTree, 8, selNode));
            popMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}